cmake_minimum_required(VERSION 3.22)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# config symbols (macros)
# GFL：Gd32f4xx_Firmware_Library
set(GFL_Defines_Syms
    GD32F470
)

# GFL include paths
set(GFL_Include_Dirs
    ${CMAKE_SOURCE_DIR}/App/Inc
    ${CMAKE_SOURCE_DIR}/Core/Inc
    ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Include
    ${CMAKE_SOURCE_DIR}/Drivers/CMSIS/Device/GD32F4xx/Include
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Include
)

# GFL application sources
set(GFL_Application_Src
    ${CMAKE_SOURCE_DIR}/App/Src/main.c
    ${CMAKE_SOURCE_DIR}/Core/Src/gd32f4xx_it.c
    ${CMAKE_SOURCE_DIR}/Core/Src/systick.c
    ${CMAKE_SOURCE_DIR}/Core/Src/syscalls.c
    ${CMAKE_SOURCE_DIR}/Core/Src/sysmem.c
    ${CMAKE_SOURCE_DIR}/Core/Src/startup_gd32f450_470.s
)

set(GD32_Drivers_Src
    ${CMAKE_SOURCE_DIR}/Core/Src/system_gd32f4xx.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_adc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_can.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_crc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_ctc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_dac.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_dbg.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_dci.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_dma.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_enet.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_exmc.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_exti.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_fmc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_fwdgt.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_gpio.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_i2c.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_ipa.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_iref.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_misc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_pmu.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_rcu.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_rtc.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_sdio.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_spi.c 
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_syscfg.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_timer.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_tli.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_trng.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_usart.c
    ${CMAKE_SOURCE_DIR}/Drivers/GD32F4xx_standard_peripheral/Source/gd32f4xx_wwdgt.c
)

# Drivers Midllewares


# Link directories setup
set(GFL_LINK_DIRS

)
# Project static libraries
set(GFL_LINK_LIBS 
    GD32_Drivers
    
)

# Interface library for includes and symbols
add_library(GD32F4xx_Firmware_Library INTERFACE)
target_include_directories(GD32F4xx_Firmware_Library INTERFACE ${GFL_Include_Dirs})
target_compile_definitions(GD32F4xx_Firmware_Library INTERFACE ${GFL_Defines_Syms})

# Create GD32_Drivers static library
add_library(GD32_Drivers OBJECT)
target_sources(GD32_Drivers PRIVATE ${GD32_Drivers_Src})
target_link_libraries(GD32_Drivers PUBLIC GD32F4xx_Firmware_Library)

# Add GFL application sources to the project
target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${GFL_Application_Src})

# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${GFL_LINK_DIRS})

# Add libraries to the project
target_link_libraries(${CMAKE_PROJECT_NAME} ${GFL_LINK_LIBS})

# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)

# Validate that STM32CubeMX code is compatible with C standard
if((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99))
    message(ERROR "Generated code requires C11 or higher")
endif()
